import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
import {
	homeApi,
	userApi,
} from '../ajax/api'
import Ajax from '../ajax/index'
const store = new Vuex.Store({
	state: {
		isLoginStatus: false, // 是否登录授权
		eventListGroup: [], // 所有
		number: 1,
		storeUserInfo: {} // 用户详情
	},
	mutations: {
		goLogin(state) {
			if (!uni.getStorageSync('userid')) { // 如果没登录信息，跳转去授权
				uni.redirectTo({
					url: '/pages/user/login'
				})
			}
		},
		setLoginStatus(state, data) { // 是否设置登录状态
			state.isLoginStatus = data
		},
		setEventListGroup(state, data) {
			console.log('data...', data)
			state.eventListGroup = data
		},
		setUserInfo(state, data) {
			console.log('data...', data)
			state.storeUserInfo = data
		},
		addmber(state) {
			console.log('addmber...')
			state.number = ++state.number
		},
		change: function(state, nickname) {
			console.log('nickname...')
			state.nickname = nickname;
		}
	},
	actions: {
		// 获取用户详情
		async getStoreUserInfo({
			commit
		}) {
			let {
				code,
				data
			} = await Ajax({
				url: userApi.userInfo
			})
			if (code === 0) {
				// data.membership = 3
				commit('setLoginStatus', true)
				commit('setUserInfo', data)
			}
		},
		// 所有事件
		async getEventListGroup({
			commit
		}) {
			let {
				code,
				data
			} = await Ajax({
				url: homeApi.eventList
			})
			if (code === 0) {
				commit('setEventListGroup', data)
			}
		}

	}
})
export default store
